﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AllInOne.Entity;
using ArcSoftFace.Entity;

namespace AllInOne.Pages
{
    /// <summary>
    /// TablePage.xaml 的交互逻辑
    /// </summary>
    public partial class TablePage : Page
    {

        public MainWindow parent;

        public bool loading = false;


        public TablePage()
        {
            InitializeComponent();
        }


        public TablePage(MainWindow parent)
        {
            InitializeComponent();
            this.parent = parent;
        }

        public void InitPage()
        {
            //this.wbTable.Visibility = Visibility.Visible;
            this.imgTitle.Source = new BitmapImage(new Uri(this.parent.businessInfo.getImgTitleUrl()));
            string url = "";
            BusinessInfo businessInfo = this.parent.businessInfo;
            switch (businessInfo.businessType)
            {
                case Entity.BusinessType.QMHZ:
                case Entity.BusinessType.SHHZ:
                case Entity.BusinessType.YSBZ:
                    DrivingLicence drivingLicence = this.parent.drivingLicence;
                    url = string.Format("{0}/cjg/table1?sfzmhm={1}&xm={2}&dabh={3}&xb={4}&csrq={5}&gj={6}&yjdz={7}&gddh={8}&sjhm={9}&yzbm={10}&qmId={11}&zpId={12}&ywlx={13}"
                        , ServiceApi.BASE_URL, drivingLicence.sfzmhm, drivingLicence.xm, drivingLicence.dabh, drivingLicence.xbmc, drivingLicence.csrq, "中国", drivingLicence.lxzsxxdz, drivingLicence.lxdh, businessInfo.sjhm!=null?businessInfo.sjhm:drivingLicence.sjhm , drivingLicence.lxzsyzbm, businessInfo.qmId, businessInfo.zpId, (int)businessInfo.businessType);
                    break;
                case BusinessType.LNMJ:
                    CarInfo carInfo = this.parent.carInfo;
                    url = string.Format("{0}/cjg/table2?xm={1}&yzbm={2}&yjdz={3}&sjhm={4}&gddh={5}&qmId={6}&hphm=鲁{7}"
                        ,ServiceApi.BASE_URL,carInfo.syr,carInfo.yzbm,carInfo.zsxxdz,carInfo.sjhm,carInfo.lxdh,businessInfo.qmId,carInfo.hphm);
                    break;
            }

            SimpleConsole.WriteInfo(url, this);
            //this.wbTable.Address = url;
            this.parent.PlaySound(Properties.Resources.male_confirm_submit, Properties.Resources.female_confirm_submit);

            loading = false;

        }


        public void ShowTable() {
            //this.wbTable.Visibility = Visibility.Visible;

        }

        public void HideTable() {
            //this.wbTable.Visibility = Visibility.Hidden;

        }

        protected string UrlEncode(string url)
        {
            byte[] bs = Encoding.GetEncoding("GB2312").GetBytes(url);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bs.Length; i++)
            {
                if (bs[i] < 128)
                    sb.Append((char)bs[i]);
                else
                {
                    sb.Append("%" + bs[i++].ToString("x").PadLeft(2, '0'));
                    sb.Append("%" + bs[i].ToString("x").PadLeft(2, '0'));
                }
            }
            return sb.ToString();
        }

        private void BtnSubmit_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (this.loading)
            {
                return;
            }

            this.loading = true;
            this.parent.ShowLoading();
            switch (this.parent.businessInfo.businessType)
            {
                case BusinessType.YSBZ:
                case BusinessType.QMHZ:
                case BusinessType.SHHZ:
                    this.parent.SubmitBzhz();
                    break;
                case BusinessType.LNMJ:
                    this.parent.SubmitLnmj();
                    break;
            }
            this.parent.HideLoading();
        }

        private void BtnBackToIndex_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.parent.ResetIndex();
        }

        private void BtnCancel_MouseUp(object sender, MouseButtonEventArgs e)
        {
            //this.wbTable.Visibility = Visibility.Hidden;
            this.parent.Confirm("提示", "取消申请?", 
                delegate () {
                    //this.wbTable.Visibility = Visibility.Visible;
                    this.parent.ResetIndex();
                },
                delegate () {
                    //this.wbTable.Visibility = Visibility.Visible;

                });
        }
    }
}
